package com.yomahub.liteflow.example;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

@SpringBootApplication
@Slf4j
public class LiteflowExampleApplication extends SpringBootServletInitializer {

    public static void main(String[] args) throws UnknownHostException {


        ConfigurableApplicationContext application = SpringApplication.run(LiteflowExampleApplication.class, args);

        Environment env = application.getEnvironment();
        String contextPath = env.getProperty("server.servlet.context-path");
        if(StrUtil.isBlank(contextPath)){
            contextPath = "";
        }

        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\thttp://localhost:{}\n\t" +
                        "External: \thttp://{}:{}\n\t"+
                        "Doc: \thttp://{}:{}/doc.html\n"+
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port") + contextPath,
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port") + contextPath);


        log.info("spring.profiles.active: {}", env.getProperty("spring.profiles.active"));
        // 获取虚拟机变量的值
        String vmVariableValue = System.getProperty("spring.profiles.active");
        log.info("虚拟机变量值：{}", vmVariableValue);
        // 获取环境变量的值
        String envVariableValue2 = System.getProperty("nacos_namespace");
        System.out.println("yaml系统变量值：" + envVariableValue2);

        // 获取环境变量的值
        String envVariableValue = System.getenv("nacos_namespace");
        log.info("环境变量值：{}", envVariableValue);
        // 获取程序变量的值
        log.info("程序变量值：{}", Arrays.stream(args).toArray().toString());

    }

    // @Override
    // protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    //     return builder.sources(LiteflowExampleApplication.class);
    // }
}
